package com.mall.controller.backend;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.google.common.collect.Maps;
import com.mall.common.Const;
import com.mall.common.ResponseCode;
import com.mall.common.ServerResponse;
import com.mall.pojo.MmallProductWithBLOBs;
import com.mall.pojo.MmallUser;
import com.mall.service.IFileService;
import com.mall.service.IProductService;
import com.mall.service.IUserService;
import com.mall.util.PropertiesUtil;

@Controller
@RequestMapping("/manage/product/")
public class ProductManageController {
	
	@Autowired
	private IUserService userService;
	
	@Autowired
	private IProductService productService;
	
	@Autowired
	private IFileService fileService;
	
	@RequestMapping("/save")
	@ResponseBody
	public ServerResponse<String> addorupdateProduct(HttpSession session, MmallProductWithBLOBs product){
		
		MmallUser  user = (MmallUser)session.getAttribute(Const.CURRENT_USER);
		//检测用户是否登录
		if(user == null){
			return ServerResponse.createByErroerCodeMessage(ResponseCode.NEED_LOGIN.getCode(),"用户未登录,请登录");
		}
		//检测用户是否是管理员
		if(userService.checkAdminRole(user).isSuccess()){
			
			//是管理员  执行插入或更新操作
			return productService.addorUpdateProduct(product);
		}else{
			return ServerResponse.createByErrorMessage("无权限操作");
		} 
	}
	
	@RequestMapping("/set_sale_status")
	@ResponseBody			//产品上下架
	public ServerResponse<String> setProductStatus(HttpSession session,Integer productId,Integer status){
		
		MmallUser  user = (MmallUser)session.getAttribute(Const.CURRENT_USER);
		//检测用户是否登录
		if(user == null){
			return ServerResponse.createByErroerCodeMessage(ResponseCode.NEED_LOGIN.getCode(),"用户未登录,请登录");
		}
		//检测用户是否是管理员
		if(userService.checkAdminRole(user).isSuccess()){
			//是管理员  
			return productService.setProductStatus(productId,status);
			
		}else{
			return ServerResponse.createByErrorMessage("无权限操作");
		} 
	}
	
	@RequestMapping("/detail")
	@ResponseBody
	public ServerResponse getDetail(HttpSession session , Integer productId){
		
		MmallUser  user = (MmallUser)session.getAttribute(Const.CURRENT_USER);
		//检测用户是否登录
		if(user == null){
			return ServerResponse.createByErroerCodeMessage(ResponseCode.NEED_LOGIN.getCode(),"用户未登录,请登录");
		}
		//检测用户是否是管理员
		if(userService.checkAdminRole(user).isSuccess()){
			
			//是管理员 , 执行业务方法
			return productService.getDeatil(productId);
		}else{
			return ServerResponse.createByErrorMessage("无权限操作");
		}  
				
	}
	
	@RequestMapping("/list")
	@ResponseBody  				//产品list
	public ServerResponse getList(HttpSession session,@RequestParam(value="pageNum",defaultValue="1")int pageNum ,@RequestParam(value="pageSize",defaultValue="10")int pageSize){
		
		MmallUser user = (MmallUser)session.getAttribute(Const.CURRENT_USER);
		//检测用户是否登录
		if(user == null){
			return ServerResponse.createByErroerCodeMessage(ResponseCode.NEED_LOGIN.getCode(),"用户未登录,请登录");
		}
		//检测用户是否是管理员
		if(userService.checkAdminRole(user).isSuccess()){
			
			//是管理员 , 执行业务方法
			return productService.getList(pageNum,pageSize);
		}else{ 
			return ServerResponse.createByErrorMessage("无权限操作");
		}  
	}
	
	@RequestMapping("/search")
	@ResponseBody
	public ServerResponse searchProduct(HttpSession session,String productName, Integer productId,@RequestParam(value="pageNum",defaultValue="1")int pageNum ,@RequestParam(value="pageSize",defaultValue="10")int pageSize){
		
		MmallUser user = (MmallUser)session.getAttribute(Const.CURRENT_USER);
		if(user == null){
			return ServerResponse.createByErroerCodeMessage(ResponseCode.NEED_LOGIN.getCode(),"用户未登录,请登录");
		}
		//检测用户是否是管理员
		if(userService.checkAdminRole(user).isSuccess()){
			
			//是管理员 , 执行业务方法
			return productService.searchProduct(productName, productId,pageNum, pageSize);
		}else{ 
			return ServerResponse.createByErrorMessage("无权限操作");
		}  
	} 
	
	@RequestMapping("/upload")
	@ResponseBody
	public ServerResponse upload(HttpSession session ,@RequestParam(value="upload_file",required=false) MultipartFile file,HttpServletRequest request){
		MmallUser user = (MmallUser)session.getAttribute(Const.CURRENT_USER);
		if(user == null){
			return ServerResponse.createByErroerCodeMessage(ResponseCode.NEED_LOGIN.getCode(),"用户未登录,请登录");
		}
		
		//检测用户是否是管理员
		if(userService.checkAdminRole(user).isSuccess()){
			
			//是管理员 , 执行上传操作.
			String path = request.getSession().getServletContext().getRealPath("upload");
			System.out.println(path); 
			String targetFileName = fileService.upload(file, path);
			String url = PropertiesUtil.getProperty("ftp.server.http.prefix")+targetFileName;
			Map map = new HashMap();
			map.put("uri", targetFileName);
			map.put("url", url);
			return ServerResponse.createBySuccess(map);
		}else{ 
			return ServerResponse.createByErrorMessage("无权限操作");
		}  
		
	}
	
	/** 
	 	
	  	使用的富文本编辑器是: simditor ,所以要按照要求返回格式:
	  	  {
	  		"success": true/false,
			    "msg": "error message", (可选项)
			"file_path": "real path"
	  	  }
	*/
	@RequestMapping("/richtext_img_upload")
	@ResponseBody		//富文本上传图片
	public Map richUpload(HttpSession session ,@RequestParam(value="upload_file",required=false) MultipartFile file,HttpServletRequest request,HttpServletResponse response){
		
		Map map = new HashMap();
		MmallUser user = (MmallUser)session.getAttribute(Const.CURRENT_USER);
		if(user == null){
			map.put("success", false);
			map.put("msg", "用户未登录");
			return map;
		}
		 
		//检测用户是否是管理员
		if(userService.checkAdminRole(user).isSuccess()){
			  
			//是管理员 , 执行上传操作.
			String path = request.getSession().getServletContext().getRealPath("upload");
			System.out.println(path); 
			
			String targetFileName = fileService.upload(file, path);
			if(StringUtils.isBlank(targetFileName)){
				map.put("success", false);
				map.put("msg", "上传失败");
				return map;
			}
			
			String url = PropertiesUtil.getProperty("ftp.server.http.prefix")+targetFileName;
			map.put("success", true);
			map.put("msg", "上传成功");
			map.put("file_path", url);
			response.setHeader("Access-Control-Allow-Headers", "X-File-Name");
			return map;
			
		}else{
			map.put("success", false);
			map.put("msg", "用户不是管理员,无权限");
			return map;
		}  
	}

	
	
}
